package com.ebc.eservice.controller.app;

import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.EbcCardService;
import com.ebc.eservice.service.app.PersonCardService;
import com.ebc.eservice.service.cardStat.CardStatService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.util.common.constant.ApiTextHelperConstant;
import com.ebc.eservice.util.common.constant.DeveloperConstant;
import com.ebc.eservice.vo.card.CardStatReqVo;
import com.ebc.eservice.vo.card.Message.CardMessageReqVo;
import com.ebc.eservice.vo.card.Message.CardMessageRespVo;
import com.ebc.eservice.vo.card.history.CardHistoryReqVo;
import com.ebc.eservice.vo.card.pic.CardPicVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Joey
 * @Description:名片信息
 * @date:2022/9/23 16:08
 */
@Api(tags = "小程序-个人名片-[基本信息][留言][个人简介][四个按钮]")
@RestController
@RequestMapping("/appCard")
public class PersonCardController {

    @Autowired
    private PersonCardService ebcCardService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private PersonCardService messageService;
    @Autowired
    private CardStatService cardStatService;

    @ApiOperation(value = "获取名片信息", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/person")
    public JsonResult getCardTest(Integer staffId){
        return  ebcCardService.getPersonCard(staffId);
    }

    @ApiOperation(value = "获取照片显示", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping(value = "/getPic",produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_GIF_VALUE}) // 支持jpg\png\gif
    @ResponseBody
    public byte[] getPic(Integer cardId){
        System.out.println("=====正在调用简介照片:"+cardId);
        byte[] data = null;
        //条件查询器
        Query query = Query.query(Criteria.where("cardId").is(cardId));
        UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
        if(file != null){
            data = file.getContent().getData();//blob类型
        }else{
            Query queryError = Query.query(Criteria.where("cardId").is(100));
            UploadFile files = mongoTemplate.findOne(queryError,UploadFile.class);
            if(files != null){
                data = files.getContent().getData();
            }
        }
        //byte[]
        return data;
    }

    @ApiOperation(value = "添加留言", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/addMessage")
    public JsonResult addMessage(CardMessageReqVo mReqVo){
        System.out.println("========正在调用添加留言, data=> "+mReqVo);
        return  messageService.addMessage(mReqVo);
    }

    @ApiOperation(value = "回复留言", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/replyMessage")
    public JsonResult replyMessage(CardMessageRespVo mRespVo){
        return  messageService.replyMessage(mRespVo);
    }

    @ApiOperation(value = "根据名片id用户id获取留言回复列表-仅公开", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/getOpenMsgList")
    public JsonResult getMsgList(Integer cardId,Integer userId){
        return  messageService.getMsgByCardIdUserId(cardId,userId);
    }

    //保存名片
    @ApiOperation(value = "保存名片", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/bt/saveCardByUc")
    public JsonResult DownloadCard(CardStatReqVo cardStatVo){
        int judge = cardStatService.roleJudge(cardStatVo);
        if(judge == 1){
            return  JsonResult.success("员工账号,无需记录");
        }
        return  cardStatService.saveCard(cardStatVo);
    }

    //添加通讯录
    @ApiOperation(value = "添加通讯录", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/bt/addCardToContact")
    public JsonResult addCardToContact(CardStatReqVo cardStatVo){
        int judge = cardStatService.roleJudge(cardStatVo);
        if(judge == 1){
            return  JsonResult.success("员工账号,无需记录");
        }
        return  cardStatService.addContacts(cardStatVo);
    }
    //复制微信号
    @ApiOperation(value = "复制微信号", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/bt/copyWxAccount")
    public JsonResult copyWxAccount(CardStatReqVo cardStatVo){
        int judge = cardStatService.roleJudge(cardStatVo);
        if(judge == 1){
            return  JsonResult.success("员工账号,无需记录");
        }
        return  cardStatService.copyWeChat(cardStatVo);
    }
    //转发名片
    @ApiOperation(value = "转发名片", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/bt/reposeCard")
    public JsonResult reposeCard(CardStatReqVo cardStatVo){
        int judge = cardStatService.roleJudge(cardStatVo);
        if(judge == 1){
            return  JsonResult.success("员工账号,无需记录");
        }
        return  cardStatService.reposeCard(cardStatVo);
    }






}
